package com.csw.shuanfa.SuanFa;

import com.csw.shuanfa.entity.Order;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1) 对orderList 按shop分组；
 * 2)  求各个shop的value之和；
 * 3) 求各个shop的value的平均值，保留2位小数
 * 4)求各个shop的value最大、最小值
 * 考虑真实情况下，可能有的order的属性是null，这种order不作处理，
 * 尽量使用最简的运算复杂度，循环遍历越少越好"
 */
public class TestOrder {

    private static BigDecimal value;

    public static void main(String[] args) {
        List<Order> list = new ArrayList<>();
        //{1,""shopA"",10},{2,""shopB"",10},{3,""shopB"",20},{4,""shopC"",10},{5,""shopC"",20},{6,""shopC"",30}，{7,""shopC"",null}.
        list.add(new Order(1, "shopA", new BigDecimal(10)));
        list.add(new Order(2, "shopB", new BigDecimal(10)));
        list.add(new Order(3, "shopB", new BigDecimal(20)));
        list.add(new Order(4, "shopC", new BigDecimal(10)));
        list.add(new Order(5, "shopC", new BigDecimal(20)));
        list.add(new Order(6, "shopC", new BigDecimal(30)));
        list.add(new Order(7, "shopC", null));

//1

        Map<String, List<Order>> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            Order order = list.get(i);
            String key = order.getShop();
            if (map.get(key) == null) {
                List list0 = new ArrayList();
                list0.add(order);
                map.put(key, list0);
            } else {
                List list1 = map.get(key);
                list1.add(order);
                map.put(key, list1);
            }
        }
        for (String s : map.keySet()) {
            //s + "中含有" + map.get(s).toString());
        }
        //----------------------------------------------------------");
        //2
        Map<String, Double> map2 = new HashMap();
        //3
        Map<String, Object> map3 = new HashMap<>();
        //4
        List list4 = new ArrayList();
        for (String s : map.keySet()) {
            List list3 = map.get(s);
            double sum = 0;
            double max = 0;
            double min = 0;
            for (int i = 0; i < list3.size(); i++) {
                Order order = (Order) list3.get(i);
                BigDecimal value00 = order.getValue();

                if (value00 != null) {
                    double vDD = Double.parseDouble(value00.toString());
                    sum = sum + vDD;

                    if (i == 0) {
                        max = vDD;
                        min = vDD;
                    } else {
                        if (vDD > max) {
                            max = vDD;
                        }
                        if (vDD < min) {
                            min = vDD;
                        }
                    }
                }
            }
            map2.put(s, sum);


            BigDecimal b = new BigDecimal(0.2);
            double v1 = sum / list3.size();
            map3.put(s, v1);


            List list40 = new ArrayList();
            list40.add(s);
            list40.add(max);
            list40.add(min);
            list4.add(list40);
        }
        for (String s : map2.keySet()) {
            //s + "value之和为" + map2.get(s).toString());
        }
        //----------------------------------------------------------");
        for (String s : map3.keySet()) {
            //s + "平均数为" + map3.get(s).toString());
        }
        //----------------------------------------------------------");
        for (int i = 0; i < list4.size(); i++) {
            List childList = (List) list4.get(i);
            //childList.get(0) + "最大为" + childList.get(1) + "最小为" + childList.get(2));
        }


    }
}
